<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>轮播图练习2</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        li {
            list-style: none;
        }
        
        #div1 {
            width: 850px;
            height: 500px;
            margin: 50px auto;
            overflow: hidden;
            position: relative;
        }
        
        #div1 ul li {
            height: 500px;
            opacity: 0;
            position: absolute;
            left: 0;
            top: 0;
            z-index: 0;
            transition: opacity 1.5s;
        }
        
        #div1 ul li.ac {
            z-index: 5;
            opacity: 1;
        }
        
        #div1 ol {
            position: absolute;
            right: 10%;
            bottom: 10px;
            z-index: 6
        }
        
        #div1 ol li {
            width: 20px;
            height: 20px;
            background: #fff;
            margin-left: 5px;
            float: left;
            line-height: 20px;
            text-align: center;
            cursor: pointer;
        }
        
        #div1 ol li.ac {
            background: red;
            color: #fff;
        }
        
        #div1>a {
            text-decoration: none;
            position: absolute;
            top: 50%;
            margin-top: -10px;
            height: 40px;
            line-height: 32px;
            text-align: center;
            width: 40px;
            font-size: 40px;
            vertical-align: middle;
            color: #fff;
            background: rgba(0, 0, 0, 0.5);
            z-index: 6;
        }
        
        #goPrev {
            left: 0;
        }
        
        #goNext {
            right: 0;
        }
        
        img {
            width: 850px;
            height: 500px;
        }
    </style>
    <script>
        window.onload = function() {
            var div = document.querySelector('#div1');
            imgs = document.querySelectorAll('ul li');
            btns = document.querySelectorAll('ol li');
            goprev = document.querySelector('#goPrev');
            gonext = document.querySelector('#goNext');
            var index = 0;
            var lastindex = 0;
            Array.from(btns).forEach((btn, i) => {
                    btn.onclick = function() {
                        lastindex = index;
                        index = i;
                        btns[lastindex].classList.remove('ac')
                        btns[index].classList.add('ac')
                        imgs[lastindex].classList.remove('ac')
                        imgs[index].classList.add('ac')
                    }
                })
                //向后切换
            gonext.onclick = function() {
                    lastindex = index;
                    index++;
                    if (index === btns.length) {
                        index = 0;
                    }
                    btns[lastindex].classList.remove('ac');
                    btns[index].classList.add('ac');
                    imgs[lastindex].classList.remove('ac');
                    imgs[index].classList.add('ac');
                }
                //向前切换
            goprev.onclick = function() {
                lastindex = index;
                index--;
                if (index < 0) {
                    index = btns.length - 1;
                }
                btns[lastindex].classList.remove('ac');
                btns[index].classList.add('ac');
                imgs[lastindex].classList.remove('ac');
                imgs[index].classList.add('ac');
            }

            function auto() {
                div.timer = setInterval(() => {
                    gonext.onclick();
                }, 3000)
            }
            auto();
            //当鼠标放上去停止
            div.onmouseenter = function() {
                    clearInterval(div.timer)
                }
                //当鼠标离开
            div.onmouseleave = auto;
        }
    </script>
</head>

<body>
    <div id="div1">
        <ul>
            <li class="ac">
                <a href="javascript:alert(0);"><img src="images/1.jpg" /></a>
            </li>
            <li>
                <a href="javascript:alert(1);"><img src="images/2.jpg" /></a>
            </li>
            <li>
                <a href="javascript:alert(2);"><img src="images/3.jpg" /></a>
            </li>
            <li>
                <a href="javascript:alert(3);"><img src="images/4.jpg" /></a>
            </li>
            <li>
                <a href="javascript:alert(4);"><img src="images/5.jpg" /></a>
            </li>
        </ul>
        <ol>
            <li class="ac">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ol>
        <a href="javascript:;" id="goPrev">&laquo;</a>
        <a href="javascript:;" id="goNext">&raquo;</a>
    </div>
</body>

</html>